package com.ytktt.study.week8.task2;

import com.google.common.collect.Lists;
import com.ytktt.study.week8.task2.entity.OrderInfoEntity;
import com.ytktt.study.week8.task2.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据分片测试项目启动器
 *
 * @author ytk
 */
@Slf4j
@SpringBootApplication
@MapperScan("com.ytktt.study.week8.task2.mapper")
public class ShardingTestApplication implements ApplicationRunner {

    @Autowired
    private OrderService orderService;

    public static void main(String[] args) {
        SpringApplication.run(ShardingTestApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 批量插入一波测试
        List<OrderInfoEntity> list = Lists.newArrayList();
        BigDecimal itemPrice = new BigDecimal(1);
        BigDecimal orderPrice = new BigDecimal(2);
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 100; i++) {
            OrderInfoEntity entity = new OrderInfoEntity();
            entity.setUserId(i % 2 + 1);
            entity.setItemId(1);
            entity.setItemPrice(itemPrice);
            entity.setAmount(2);
            entity.setOrderPrice(orderPrice);
            entity.setDelFlag((byte) 0);
            entity.setStatus((byte) 1);
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            list.add(entity);
        }
        this.orderService.batchSave(list);

        // 单个插入几个测试
        BigDecimal itemPrice1 = new BigDecimal(4);
        BigDecimal orderPrice1 = new BigDecimal(8);
        OrderInfoEntity entity0 = getOrderInfoEntity(now, itemPrice1, orderPrice1, 1);
        this.orderService.save(entity0);
        OrderInfoEntity entity1 = getOrderInfoEntity(now, itemPrice1, orderPrice1, 2);
        this.orderService.save(entity1);
        OrderInfoEntity entity2 = getOrderInfoEntity(now, itemPrice1, orderPrice1, 2);
        this.orderService.save(entity2);

        // 查询全部
        List<OrderInfoEntity> listAll = this.orderService.list();
        System.out.println("userId 1 list: " + listAll);

        // 获益一个订单用于更新测试
        OrderInfoEntity entity = listAll.get(0);
        // 获取一个用户id用作分库数据查询
        int userId = entity.getUserId();
        entity.setAmount(100);
        entity.setUserId(null);
        entity.setOrderPrice(entity.getItemPrice().multiply(BigDecimal.valueOf(100)));
        this.orderService.updateById(entity);

        // 根据用户id分库查询
        List<OrderInfoEntity> listByUserId = this.orderService.listByUserId(userId);
        System.out.println("after update userId " + userId + " list: " + listByUserId);

        // 单独删除单个
        this.orderService.removeById(listByUserId.get(listByUserId.size() - 1).getId());

        // 再次查询
        System.out.println("after delete one userId " + userId + " list: " + this.orderService.listByUserId(userId));

        // 删除全部
        this.orderService.removeAll();

    }

    private OrderInfoEntity getOrderInfoEntity(LocalDateTime now, BigDecimal itemPrice1, BigDecimal orderPrice1, int userId) {
        OrderInfoEntity entity0 = new OrderInfoEntity();
        entity0.setUserId(userId);
        entity0.setItemId(2);
        entity0.setItemPrice(itemPrice1);
        entity0.setAmount(2);
        entity0.setOrderPrice(orderPrice1);
        entity0.setDelFlag((byte) 0);
        entity0.setStatus((byte) 1);
        entity0.setCreateTime(now);
        entity0.setUpdateTime(now);
        return entity0;
    }
}
